﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;



namespace FrmHotel
{
    public partial class FrmRuzhu : Form
    {
        public int id;//接收主菜单的房间号 

        public FrmRuzhu()
        {
            InitializeComponent();
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            this.button1.BackColor = Color.FromArgb(249, 102, 34);
        }

       

        private void button3_MouseEnter(object sender, EventArgs e)
        {
            this.button3.BackColor = Color.FromArgb(249, 102, 34);
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            this.button3.BackColor = Color.FromArgb(69, 94, 100);
        }

       

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            this.button1.BackColor = Color.FromArgb(69, 94, 100);
        }

        public void ComdoBox()
        {
            string sql = "select * from  CetaeClass";
            DataTable dt = DBHelper.SelectForTable(sql);
            DataRow dr = dt.NewRow();
            dr["CetaeId"] = 0;
            dr["CetaeName"] = "请选择";
            dt.Rows.InsertAt(dr,0);
            this.comboBox1.DisplayMember = "CetaeName";
            this.comboBox1.ValueMember = "CetaeId";
            this.comboBox1.DataSource = dt;

            string sql2 = "select * from  PayClass";
            DataTable dt2 = DBHelper.SelectForTable(sql2);
            DataRow dr2 = dt2.NewRow();
            dr2["PayId"] = 0;
            dr2["PayName"] = "请选择";
            dt2.Rows.InsertAt(dr2, 0);
            this.comboBox3.DisplayMember = "PayName";
            this.comboBox3.ValueMember = "PayId";
            this.comboBox3.DataSource = dt2;

        }

        private void FrmRuzhu_Load(object sender, EventArgs e)
        {
            ComdoBox();
            BoinDataGridView();
            string o3 = DBHelper.SelectOne(string.Format("select count(1) from CetaeInfo ")).ToString();
            this.label15.Text = o3;
            string sql = string.Format("select r.RoomName from Room r,RoomInfo o where o.RmRoom=r.RoomId and o.RmId={0}", id);
            string r = DBHelper.SelectOne(sql).ToString();
            this.textBox3.Text =r;
            string sql1 = string.Format("select r.RoomMoney from Room r,RoomInfo o where o.RmRoom=r.RoomId and o.RmId={0}", id);
            string r1 = DBHelper.SelectOne(sql1).ToString();
            this.txt_feiyong.Text = r1;
        }

         public void BoinDataGridView()
        {
         
            this.dataGridView1.AutoGenerateColumns = false;
            string sql = "select c.CeFangHao,o.RoomName,o.RoomMoney,c.CeName,c.CeCut from  RoomInfo r,Room o,CetaeInfo c where r.RmRoom=o.RoomId and c.CeFangHao=r.RmId and 1=1";
            string hao = this.textBox1.Text;
            if (!string.IsNullOrEmpty(hao))
            {
                sql += string.Format(" and c.CeFangHao like '%{0}%' ", hao);
            }
            string name = this.textBox2.Text;
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and c.CeName like '%{0}%' ", name);
            }
            this.dataGridView1.DataSource =DBHelper.SelectForTable(sql);
        }
        private void button1_Click(object sender, EventArgs e)
        {                           
            string name = this.txt_name.Text;//主客名称        
            string gender = this.radioButton1.Checked ? "男" : "女";//性别
            int age =Convert.ToInt32( this.numericUpDown1.Value);//年龄
            int zhengjian = Convert.ToInt32(this.comboBox1.SelectedValue);//证件类型
            string bianhaoH = this.txt_zhengjainhao.Text;//证件编号
            string Address = this.txt_Address.Text;//住址
            string hoper = this.txt_hoper.Text;//联系电话
             decimal money =Convert.ToDecimal( this.txt_feiyong.Text);//房间金额

            int Dey = Convert.ToInt32(this.numericUpDown2.Value);//预住的天数
            //预离时间
            DateTime ruzhu = DateTime.Now;
            string d = ruzhu.AddDays(Dey).ToString("yyy-MM-dd 12:00:00"); ;

            string cha = string.Format("select DATEDIFF(DAY,'{0}','{1}')", ruzhu, d);
            int result1 = Convert.ToInt32(DBHelper.SelectOne(cha));
            decimal he = Convert.ToDecimal(result1 * money);

            int fnagshi = Convert.ToInt32(this.comboBox3.SelectedValue);//收款方式
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(hoper) || string.IsNullOrEmpty(bianhaoH) || string.IsNullOrEmpty(Address))
            {
                MessageBox.Show("请填写完整的入住信息！","提示信息！");
                return;
            }
            string sql5 = string.Format("select count(1) from RoomInfo r,CetaeInfo c where c.CeFangHao=r.RmId and RmState=3 and RmId={0} and c.CeName='{1}'", id, name);
            int rer1 = Convert.ToInt32(DBHelper.SelectOne(sql5));
            if (rer1 >= 1)
            {
                MessageBox.Show("此宾客已入住！", "提示信息！");
                return;
            }
            string ooo = string.Format("insert into Tongji values({0},'{1}')", he, ruzhu);//统计
            int ererer = DBHelper.ExecuteUpdate(ooo);

            string sql = string.Format(@"insert into CetaeInfo values({0},'{1}','{2}',{3},{4},'{5}','{6}','{7}','{8}',{9})",
                                    id, name, gender, age, zhengjian, bianhaoH, hoper, Address, d, 2);
            int Z1 = Convert.ToInt32(DBHelper.ExecuteUpdate(sql));
           
            string sql2 = string.Format("select CeId from CetaeInfo where CeName='{0}'", name);
            int r =Convert.ToInt32( DBHelper.SelectOne(sql2));

            string sql3 = string.Format(@"insert into checkInfo values('{0}','{1}',default,{2},{3},{4})", r, ruzhu, he, fnagshi, 1);
            int Z2 = Convert.ToInt32(DBHelper.ExecuteUpdate(sql3));
            if (Z1 > 0&&Z2>0 )
            {

                string sql4 = string.Format("update RoomInfo set RmState=3 where RmId={0}", id);
                int s = DBHelper.ExecuteUpdate(sql4);
                if (s > 0)
                {
                    MessageBox.Show("入住成功！","提示信息！");
                    BoinDataGridView();
                    string o3 = DBHelper.SelectOne(string.Format("select count(1) from CetaeInfo ")).ToString();
                    this.label15.Text = o3;
                }
            }
            else
            {
                MessageBox.Show("入住失败！", "提示信息！");
                BoinDataGridView();
                return;
            }
        }
        private void button4_MouseEnter(object sender, EventArgs e)
        {
            this.button4.BackColor = Color.FromArgb(249, 102, 34);
        }

        private void button4_MouseLeave(object sender, EventArgs e)
        {
            this.button4.BackColor = Color.FromArgb(69, 94, 100);
        }

      
        private void button4_Click(object sender, EventArgs e)
        {
            BoinDataGridView();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string name = this.txt_name.Text;//主客名称
            string bianhaoH = this.txt_zhengjainhao.Text;//证件编号
            string Address = this.txt_Address.Text;//住址
            if (string.IsNullOrEmpty(name)||string.IsNullOrEmpty(bianhaoH)||string.IsNullOrEmpty(Address))
            {
                MessageBox.Show("请先入住主客信息！","提示信息！");
                return;
            }
            FrmSuiKe fs = new FrmSuiKe();
            fs.id = id;
            fs.ShowDialog();
        }

        private void label18_Click(object sender, EventArgs e)
        {
            string name = this.txt_name.Text;//主客名称
            string bianhaoH = this.txt_zhengjainhao.Text;//证件编号
            string Address = this.txt_Address.Text;//住址
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(bianhaoH) || string.IsNullOrEmpty(Address))
            {
                MessageBox.Show("请先入住主客信息！", "提示信息！");
                return;
            }
            FrmSuiKe fs = new FrmSuiKe();
            fs.id = id;
            fs.ShowDialog();
        }
        
    }
}
